package com.gzdh.device.task.job;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gzdh.device.comm.util.HttpClientUtils;
import com.gzdh.device.shop.entity.TradeNo;
import com.gzdh.device.shop.service.TradeNoService;
import com.gzdh.device.task.entity.TaskCenter;
import com.gzdh.device.task.service.TaskCenterService;
import com.gzdh.device.wx.entity.WxSubMchid;
import com.gzdh.device.wx.service.WxSubMchidService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

import static java.lang.Thread.sleep;

@Slf4j
@Component
public class SyncOrderBillJob {

    @Autowired
    WxSubMchidService wxSubMchidService;

    @Autowired
    TradeNoService tradeNoService;

    @Autowired
    TaskCenterService taskCenterService;

    @XxlJob("SyncOrderBillJobHandler")
    public void SyncOrderBillJobHandler() throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String targetDate;
        try {
            String param = XxlJobHelper.getJobParam();
            String[] methodParams = param.split(",");
            sdf.parse(methodParams[0]);
            targetDate = methodParams[0];
        } catch (Exception e) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.DATE, -1);
            Date date = calendar.getTime();
            targetDate = sdf.format(date);
        }
        List<WxSubMchid> wxSubMchidList = wxSubMchidService.list(new QueryWrapper<WxSubMchid>().lambda().eq(WxSubMchid::getSyncBill, 1));
        for (WxSubMchid goo : wxSubMchidList) {
            //需要同步的商户
            int counts = taskCenterService.count(new QueryWrapper<TaskCenter>().lambda()
                    .eq(TaskCenter::getCondition, targetDate + "-" + goo.getMerchant())
                    .eq(TaskCenter::getType, 2011));
            if (counts > 0) {
                continue;
            }
            Map<String, String> map = new HashMap<>();
            map.put("channel", goo.getMerchant());
            map.put("date", targetDate);
            String str = HttpClientUtils.doPost("https://wzy.wenhuitec.com/zf/api/system/v3/leshua/getOrdersBill",
                    map,
                    "123456789987654321");
            log.info("同步：" + str);
            if (StringUtils.isEmpty(str)) {
                continue;
            }
            JSONArray jsonArray = JSONObject.parseObject(str).getJSONArray("result");
            if (jsonArray.isEmpty()) {
                continue;
            }
            List<TradeNo> tradeNoList = jsonArray.toJavaList(TradeNo.class);
            tradeNoService.saveOrUpdateBatch(tradeNoList);
            TaskCenter taskCenter = new TaskCenter();
            taskCenter.setCondition(targetDate + "-" + goo.getMerchant());
            taskCenter.setParameter(goo.getMerchant());
            taskCenter.setType("2011");
            taskCenter.setTitle(goo.getName() + "同步对账单");
            taskCenter.setStatus(1);
            taskCenter.setDescs("对账单同步完成，总条数:" + tradeNoList.size());
            taskCenterService.save(taskCenter);
            sleep(1000);
        }
    }
}
